package org.dromara.web.service.business;


import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.dromara.common.core.constant.AppUpdateConstants;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.dao.business.domain.AppUpdate;
import org.dromara.dao.business.domain.Model;
import org.dromara.dao.business.domain.ModelQuery;
import org.dromara.dao.business.domain.bo.AppUpdateBo;
import org.dromara.dao.business.domain.vo.AppUpdateVo;
import org.dromara.dao.business.mapper.AppUpdateMapper;
import org.dromara.dao.business.mapper.ModelMapper;
import org.dromara.dao.system.domain.SysNotice;
import org.dromara.dao.system.domain.SysUser;
import org.dromara.dao.system.domain.bo.SysNoticeBo;
import org.dromara.dao.system.domain.vo.SysNoticeVo;
import org.dromara.dao.system.domain.vo.SysUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Service
public class AppUpdateService {

    @Autowired
    AppUpdateMapper appUpdateMapper;

    /**
     * 查询数据列表
     *
     * @param appUpdateBo
     * @param pageQuery
     * @return
     */
    public TableDataInfo<AppUpdateVo> queryPageList(AppUpdateBo appUpdateBo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppUpdate> lqw = buildQueryWrapper(appUpdateBo);
        Page<AppUpdateVo> page = appUpdateMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(page);
    }

    private LambdaQueryWrapper<AppUpdate> buildQueryWrapper(AppUpdateBo bo) {
        LambdaQueryWrapper<AppUpdate> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeviceType() != null, AppUpdate::getDeviceType, bo.getDeviceType());
        lqw.orderByAsc(AppUpdate::getId);
        return lqw;
    }


    /**
     * 获取最新一条数据
     *
     * @param bo
     * @return
     */
    public AppUpdateVo getLatestInfo(AppUpdateBo bo) {
        LambdaQueryWrapper<AppUpdate> lqw = Wrappers.lambdaQuery();
        lqw.eq(AppUpdate::getDeviceType, bo.getDeviceType());
        lqw.eq(AppUpdate::getStatus, AppUpdateConstants.STATUS_SHOW);
        lqw.orderByDesc(AppUpdate::getId);
        return appUpdateMapper.selectVoOne(lqw);
    }
}
